package com.juntuo.platformUtils;


import com.juntuo.Enums.Constants;
import com.juntuo.util.HttpsUtil;
import com.juntuo.util.JsonUtil;
import com.juntuo.util.StreamClosedHttpResponse;

import java.util.HashMap;
import java.util.Map;

/**
 *  Auth:
 *  This interface is used to authenticate third-party systems before third-party systems access open APIs.
 */
public class AuthenticationUtil {
    @SuppressWarnings({ "resource", "unchecked" })
    public static String login(HttpsUtil httpsUtil) throws Exception {

        String appId = Constants.APP_ID;
        String secret = Constants.APP_SECRET;
        String urlLogin = Constants.BASE_URL+Constants.AUTH_URL;

        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("secret", secret);

        StreamClosedHttpResponse responseLogin = httpsUtil.doPostFormUrlEncodedGetStatusLine(urlLogin, param);

        System.out.println("app auth success,return accessToken:");
        System.out.print(responseLogin.getStatusLine());
        System.out.println(responseLogin.getContent());
        System.out.println();

        //resolve the value of accessToken from responseLogin.
        Map<String, String> data = new HashMap<>();
        data = JsonUtil.jsonString2SimpleObj(responseLogin.getContent(), data.getClass());
        String accessToken = data.get("accessToken");
        System.out.println("accessToken:" + accessToken);
//        Cache.accessToken=accessToken;
//        Cache.refreshToken=data.get("refreshToken");
        return accessToken;

    }
}
